package database

import (
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

var DB *gorm.DB

type User struct {
	gorm.Model
	Name string
	Age  int8
}

func init() {
	db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}

	// 迁移 schema
	db.AutoMigrate(&User{})

	DB = db

	// Create
	//db.Create(&User{Name: "john", Age: 20})

	// Read
	//var user User
	//db.First(&user, 1) // 根据整形主键查找
	//db.Model(&user).Update("Age", 21)
	//db.Delete(&user, 1)
}
